package com.zkjl.protect_business.controller;


import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.dto.*;
import com.zkjl.protect_business.entity.Permission;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.service.PermissionService;
import com.zkjl.protect_business.utils.ResultUtil;
import com.zkjl.protect_business.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import static org.apache.poi.hemf.record.emfplus.HemfPlusRecordType.save;

@RestController
@RequestMapping("/permissions")
@Api(tags = {"权限"})
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @ApiOperation(value = "新增权限", notes = "新增权限", httpMethod = "POST")
    @PostMapping
    @SystemLog(description = "新增权限")
    public ApiResponse<Permission> save(@RequestBody PermissionDTO permissionDTO) {

        Permission permission = permissionService.saveOrUpdate(permissionDTO, null);

        return ApiResponse.ofSuccess(permission);
    }


    @ApiOperation(value = "权限列表", notes = "权限列表", httpMethod = "GET")
    @GetMapping
    @SystemLog(description = "查看权限列表")
    public ApiResponse<PageVO<Permission>> page(PageBaseDTO pageBaseDTO, SortDTO sortDTO) {

        PageImpl page = permissionService.page(pageBaseDTO, sortDTO);
        ApiResponse apiResponse = ResultUtil.successPagesVO(page);
        return apiResponse;
    }

    @ApiOperation(value = "修改权限", notes = "修改权限", httpMethod = "PUT")
    @PutMapping(value = "{id}")
    @SystemLog(description = "修改权限")
    public ApiResponse<Permission> update(@RequestBody PermissionDTO permissionDTO, @PathVariable String id) {

        Permission permission = permissionService.saveOrUpdate(permissionDTO, id);
        return ApiResponse.ofSuccess(permission);

    }

    @ApiOperation(value = "获取权限信息", notes = "获取权限信息", httpMethod = "GET")
    @GetMapping(value = "{id}")
    @SystemLog(description = "查看权限信息")
    public ApiResponse<Permission> get(@PathVariable String id) {
        Permission permission = permissionService.get(id);
        return ApiResponse.ofSuccess(permission);
    }


    @ApiOperation(value = "删除权限", notes = "删除权限", httpMethod = "DELETE")
    @DeleteMapping(value = "{id}")
    @SystemLog(description = "删除权限")
    public ApiResponse del(@PathVariable String id) {

        Boolean del = permissionService.del(id);
        return ApiResponse.ofSuccess();
    }


}
